home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Papers / Garrison / Code / SchmoozingExamples / Listing5.m < prev    next >
Encoding:
Text File  |  2001-06-23  |  1.5 KB  |  61 lines

  1. //
  2. //  Listing5.m
  3. //  SchmoozingExamples
  4. //
  5. //  Created by garrison on Fri Apr 20 2001.
  6. //  Copyright (c) 2001 Standard Orbit Software, LLC. All rights reserved.
  7. //
  8. //  Permission is granted to use this code for any purpose, at your own risk.
  9. //  No warranties are expressed or implied.
  10.  
  11. #import <Foundation/Foundation.h>
  12. #import <OmniNetworking/OmniNetworking.h>
  13.  
  14. #import "Connection.h"
  15.  
  16. @implementation Connection
  17.  
  18. - initWithConnectedSocket: (ONTCPSocket*) aSocket 
  19. {
  20.     // Slave Step 1. Receive the accepted socket
  21.        self = [super init];
  22.         if ( self ) {
  23.             if ( [aSocket isConnected] )
  24.                 mySocket = [aSocket retain];
  25.             else
  26.                 return nil;
  27.         }
  28.     return self;
  29. }
  30.  
  31. - (void) processConnection 
  32. {
  33.     ONSocketStream *stream = nil;
  34.     NSString *inFromClient = nil;
  35.     
  36.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  37.  
  38.     stream = [ONSocketStream streamWithSocket: mySocket];
  39.     
  40.         NSLog(@"%@ is processing the connection", [NSThread currentThread]);
  41.     inFromClient = [stream readLine];
  42.     [stream writeFormat:@"%@: %@ received connection from %@\r\n", 
  43.             [NSCalendarDate calendarDate],  
  44.             [ONHost localHostname],
  45.             [[mySocket remoteAddressHost] hostname]];
  46.     [stream writeFormat:@"%@\r\n", [inFromClient uppercaseString]];
  47.     // Slave Step 2. Interact with client.
  48.  
  49.         [mySocket abortSocket];
  50.     // Slave Step 3. Close the connection.  This server is pretty draconian.
  51.     NSLog(@"%@ has closed connection", [NSThread currentThread]);
  52.         [pool release];
  53. }
  54.  
  55.  
  56. - (void) dealloc
  57. {
  58.     [mySocket release];
  59.     [super dealloc];
  60. }
  61. @end